package cn.vw.hi.list.action;

import java.io.PrintWriter;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import cn.vw.hi.commen.model.City;
import cn.vw.hi.commen.model.Counties;
import cn.vw.hi.commen.model.Loads;
import cn.vw.hi.commen.model.Province;
import cn.vw.hi.commen.tools.Page;
import cn.vw.hi.list.manager.BusinessManager;
import cn.vw.hi.list.model.Business;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class BusinessAction extends ActionSupport {

	private BusinessManager businessManager=null;
	private Business business=null;
	private Page pageObj=null;
	private int page;
	private int rows;
	private String searchStr=null;
	private String ids;
	private List list=null;
	private int provinceId;
	private int cityId;
	private int countiesId;
	@Override
	public String execute() throws Exception {
		return "success";
	}

	//添加路名
	public String addBusiness() throws Exception{
		ActionContext.getContext().getSession().put("location", provinceId+","+cityId+","+countiesId+","+business.getLoadId());
		boolean isSuccess=businessManager.addBusiness(business);
		return "AddBusiness";
	}
	//获得商家
	public String businessListForJson() throws Exception{
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(rows);
		List list=businessManager.businessList(pageObj,searchStr);
		JSONObject json=new JSONObject();
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			Object[] obj=(Object[])list.get(i);
			Business business=(Business)obj[0];
			Province province=(Province)obj[1];
			City city=(City)obj[2];
			Counties counties=(Counties)obj[3];
			Loads load=(Loads)obj[4];
			JSONObject temp=new JSONObject();
			temp.put("id", business.getId());
			temp.put("name",business.getName());
			temp.put("address", province.getProvince()+","+city.getCity()+","+counties.getCounties()+","+load.getLoadName());
			temp.put("opt","<a href=''>查看</a>&nbsp;&nbsp;<a href='business!modifyPage.action?business.id="+business.getId()+"' >修改</a>&nbsp;&nbsp;<a href='javascript:void(0)' onclick='deleteBusiness("+business.getId()+")'>删除</a>" );
			jsonArray.add(temp);
		}
		json.put("total", pageObj.getRowCount());
		json.put("rows",jsonArray);
		backText(json.toString());
		return "Data";
	}
	//删除商家
	public String deleteBusiness() throws Exception{
		boolean isSuccess=businessManager.deleteBusiness(ids);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//修改商家
	public String modifyPage() throws Exception{
		list=this.businessManager.findBusiness(business.getId());
		return "ModifyPage";
	}
	//修改商户
	public String modifyBusiness() throws Exception{
		boolean isSuccess=this.businessManager.modifyBusiness(business);
		return "List";
	}
	//获得商户列表
	
	public String businessList() throws Exception{
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(7);
		
		 list=businessManager.businessList(pageObj,searchStr);
		return "ShowList";
	}
    //获得商户列表
	public String businessDetial() throws Exception{
		 list=businessManager.findBusiness(business.getId());
		return "Detial";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	public BusinessManager getBusinessManager() {
		return businessManager;
	}

	public void setBusinessManager(BusinessManager businessManager) {
		this.businessManager = businessManager;
	}

	public Business getBusiness() {
		return business;
	}

	public void setBusiness(Business business) {
		this.business = business;
	}

	public Page getPageObj() {
		return pageObj;
	}

	public void setPageObj(Page pageObj) {
		this.pageObj = pageObj;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSearchStr() {
		return searchStr;
	}

	public void setSearchStr(String searchStr) {
		this.searchStr = searchStr;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(int provinceId) {
		this.provinceId = provinceId;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public int getCountiesId() {
		return countiesId;
	}

	public void setCountiesId(int countiesId) {
		this.countiesId = countiesId;
	}


}
